package com.ljh.extensions.rabbit.config;

import com.ljh.extensions.rabbit.constants.Constants;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author: Themberfue
 * @date: 2025/4/30 15:50
 * @description:
 */
@Configuration
public class RabbitMQConfig {
    @Bean("ackQueue")
    public Queue ackQueue() {
        return QueueBuilder.durable(Constants.ACK_QUEUE)
                .build();
    }
    @Bean("ackExchange")
    public DirectExchange ackExchange() {
        return ExchangeBuilder.directExchange(Constants.ACK_EXCHANGE)
                .durable(true)
                .build();
    }
//    @Bean("binding")
//    public Binding binding(Exchange exchange, Queue queue) {
//        return BindingBuilder.bind(queue)
//                .to(exchange)
//                .with("ack")
//                .noargs();
//    }
    @Bean("binding1")
    public Binding binding1(@Qualifier("ackExchange") DirectExchange exchange, @Qualifier("ackQueue") Queue queue) {
        return BindingBuilder.bind(queue)
                .to(exchange)
                .with("ack");
    }

    @Bean("persistQueue")
    public Queue persistQueue() {
        return QueueBuilder.nonDurable(Constants.PERSIST_QUEUE)
                .build();
    }
    @Bean("persistExchange")
    public DirectExchange persistExchange() {
        return ExchangeBuilder.directExchange(Constants.PERSIST_EXCHANGE)
                .durable(false)
                .build();
    }
    @Bean("binding2")
    public Binding binding2(@Qualifier("persistExchange") Exchange exchange, @Qualifier("persistQueue") Queue queue) {
        return BindingBuilder.bind(queue)
                .to(exchange)
                .with("persist")
                .noargs();
    }

    @Bean("confirmQueue")
    public Queue confirmQueue() {
        return QueueBuilder.durable(Constants.CONFIRM_QUEUE)
                .build();
    }
    @Bean("confirmExchange")
    public DirectExchange confirmExchange() {
        return ExchangeBuilder.directExchange(Constants.CONFIRM_EXCHANGE)
                .durable(true)
                .build();
    }
    @Bean("binding3")
    public Binding binding3(@Qualifier("confirmExchange") DirectExchange directExchange, @Qualifier("confirmQueue") Queue queue) {
        return BindingBuilder.bind(queue)
                .to(directExchange)
                .with("confirm");
    }

    @Bean("retryQueue")
    public Queue retryQueue() {
        return QueueBuilder.durable(Constants.RETRY_QUEUE)
                .build();
    }
    @Bean("retryExchange")
    public DirectExchange retryExchange() {
        return ExchangeBuilder.directExchange(Constants.RETRY_EXCHANGE)
                .durable(true)
                .build();
    }
    @Bean("binding4")
    public Binding binding4(@Qualifier("retryExchange") DirectExchange exchange, @Qualifier("retryQueue") Queue queue) {
        return BindingBuilder.bind(queue)
                .to(exchange)
                .with("retry");
    }

    @Bean("ttlQueue")
    public Queue ttlQueue() {
        return QueueBuilder.durable(Constants.TTL_QUEUE)
                .ttl(20_000) // ? 设置队列的 TTL 值
                .build();
    }
    @Bean("ttlExchange")
    public DirectExchange ttlExchange() {
        return ExchangeBuilder.directExchange(Constants.TTL_EXCHANGE)
                .durable(true)
                .build();
    }
    @Bean("binding5")
    public Binding binding5(@Qualifier("ttlExchange") DirectExchange exchange, @Qualifier("ttlQueue") Queue queue) {
        return BindingBuilder.bind(queue)
                .to(exchange)
                .with("ttl");
    }

    @Bean("transQueue")
    public Queue transQueue() {
        return QueueBuilder.durable(Constants.TRANS_QUEUE)
                .build();
    }
}
